package com.example.flowable;

import com.example.annotations.Autowired;
import com.example.annotations.Compontent;
import com.example.annotations.Init;
import com.example.service.AppService;
import com.example.utils.PropertiesUtils;
import liquibase.pro.packaged.S;
import org.flowable.engine.*;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;


/**
 * @Classname FLowableUtils
 * @Description TODO
 * @Date 2022/8/9 15:19
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@Compontent
public class FLowableUtils {

    @Autowired
    AppService appService;

    @Init
    public void init(){
        String flag=PropertiesUtils.getProperty("eableflowable");
        if("true".equals(flag)){
            String url= appService.getPropertiesByKey("url");
            String driver=appService.getPropertiesByKey("driver");
            String username=appService.getPropertiesByKey("username");
            String password=appService.getPropertiesByKey("password");
            ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
                    .setJdbcUrl(url)
                    .setJdbcUsername(username)
                    .setJdbcPassword(password)
                    .setJdbcDriver(driver)
                    .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE)
                    .setAsyncExecutorActivate(false);
            ProcessEngine processEngine = cfg.buildProcessEngine();

            appService.putObjectByClass(ProcessEngine.class,processEngine);
        }
    }


    public  ProcessEngine getProcessEngine() {
        ProcessEngine processEngine=(ProcessEngine)appService.getObjectByClass(ProcessEngine.class);
        return processEngine;
    }

    public TaskService getTaskService() {
        ProcessEngine processEngine=getProcessEngine();
        if(processEngine!=null){
            return processEngine.getTaskService();
        }
        return null;
    }

    public RuntimeService getRuntimeService() {
        ProcessEngine processEngine = getProcessEngine();
        if (processEngine != null) {
            return processEngine.getRuntimeService();
        }
        return null;
    }

    public HistoryService getHistoryService() {
        ProcessEngine processEngine = getProcessEngine();
        if (processEngine != null) {
            return processEngine.getHistoryService();
        }
        return null;
    }

    public  RepositoryService getRepositoryService() {
        ProcessEngine processEngine = getProcessEngine();
        if (processEngine != null) {
            return processEngine.getRepositoryService();
        }
        return null;
    }

}
